import { getDialogs,setDialogStatus,deleteDialog,addDialog} from '@/services/dialog';
import { routerRedux } from 'dva/router';

export default {
  namespace: 'dialogList',
  state: {
    total: null,
    data: [],
    loading: false
  },

  effects: {
    *fetchList({payload,callback}, { call, put }) {
      const response = yield call(getDialogs,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fixDialogStatus({payload,callback}, { call, put }) {
      const response = yield call(setDialogStatus,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *delDialog({payload,callback}, { call, put }) {
      const response = yield call(deleteDialog,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    * redirect ({ payload }, { put }) {
      yield put(routerRedux.push('/dialog/add', payload));
    },
    /** speicalCaseRedirect ({ payload }, { put }) {
      yield put(routerRedux.push('/dialog/specialIndex', payload));
    },*/
    *add({payload,callback}, { call, put }) {
      const response = yield call(addDialog,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    }
  }
};
